#!/usr/bin/env php
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018\5\31 0031
 * Time: 12:34
 */

//echo function_exists('ob_gzhandler');exit;

define("WEB_ROOT", __DIR__);
$dir_api_vendor = WEB_ROOT . DIRECTORY_SEPARATOR . 'api_vendor';
$dir_data = WEB_ROOT . DIRECTORY_SEPARATOR . 'data';
function _mkdir($dir)
{
    if (!is_dir($dir)) {
        mkdir($dir);
        chmod($dir, 0777);
    }
}
_mkdir($dir_api_vendor);
$dir_sdk = $dir_api_vendor . DIRECTORY_SEPARATOR . "php_api_sdk";
if (!is_dir($dir_sdk)) {
    if (function_exists("exec")) {
        die("‘exec’函数无法使用，你可以在目录[{$dir_api_vendor}]放置php_api_sdk,git命令:<pre>git clone https://gitee.com/yishangfinance/php_api_sdk.git</pre>");
    }
    exec("git clone https://gitee.com/yishangfinance/php_api_sdk.git {$dir_sdk}");
}
$dir_sdk_logs = $dir_sdk . DIRECTORY_SEPARATOR . "yii2api" . DIRECTORY_SEPARATOR . "logs";
_mkdir($dir_sdk_logs);
_mkdir(WEB_ROOT.DIRECTORY_SEPARATOR."wwwroot".DIRECTORY_SEPARATOR."attsort");
_mkdir(WEB_ROOT.DIRECTORY_SEPARATOR."wwwroot".DIRECTORY_SEPARATOR."attach");
$file_sdk_config_bak = $dir_sdk . DIRECTORY_SEPARATOR . "yii2api" . DIRECTORY_SEPARATOR . "Config.php.copy";
$file_sdk_config = $dir_sdk . DIRECTORY_SEPARATOR . "yii2api" . DIRECTORY_SEPARATOR . "Config.php";
if (!file_exists($file_sdk_config_bak)) {
    die("没有找到sdk配置备份");
}
if (!file_exists($file_sdk_config)) {
    copy($file_sdk_config_bak, $file_sdk_config);
}
if (!is_dir($dir_data)) {
    _mkdir($dir_data);
    _mkdir($dir_data . DIRECTORY_SEPARATOR . 'backup');
    _mkdir($dir_data . DIRECTORY_SEPARATOR . 'caches');
    _mkdir($dir_data . DIRECTORY_SEPARATOR . 'logs');
    _mkdir($dir_data . DIRECTORY_SEPARATOR . 'session');
    _mkdir($dir_data . DIRECTORY_SEPARATOR . 'tmp');
}
$file_config_bak = WEB_ROOT . DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR . "ConfigLocal.php.bak";
$file_config = WEB_ROOT . DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR . "ConfigLocal.php";
if (!file_exists($file_config_bak)) {
    die("没有找到配置备份");
}
if (!file_exists($file_config)) {
    copy($file_config_bak, $file_config);
}